package com.xujianlun.cpms.utils;

import java.lang.reflect.Field;
import java.security.acl.Permission;
import java.util.Map;
import java.util.Set;

/**
 * @version 1.0
 * @Author xujianlun
 * @Date 2022/12/17 20:16
 * @注释 Map转换工具类
 */
public class MapUtil {
    /**
     * map 转 实体类
     * @param map
     * @return
     * @throws Exception
     */
    public Permission mapToBean(Map<String,Object> map)
            throws Exception
    {
        Permission permission = new Permission() {
            @Override
            public boolean equals(Object another) {
                return false;
            }

            @Override
            public String toString() {
                return null;
            }
        };
        if(map != null){
            Field[] declaredFields = Permission.class.getDeclaredFields();
            if(declaredFields != null){

                for (Field declaredField : declaredFields)
                {
                    declaredField.setAccessible(true);
                    Set<String> mapKeys = map.keySet();

                    for (String mapKey : mapKeys)
                    {

                        if(declaredField.getType().toString().contains("Integer"))//判断属性类型 进行转换,map中存放的是Object对象需要转换 实体类中有多少类型就加多少类型,实体类属性用包装类;
                        {
                            if(declaredField.getName().equals(mapKey)){
                                declaredField.set(permission,Integer.valueOf(map.get(mapKey).toString()));
                                break;
                            }
                        }
                        if(declaredField.getType().toString().contains("String") )//判断属性类型 进行转换;
                        {
                            if(declaredField.getName().equals(mapKey)){
                                declaredField.set(permission,map.get(mapKey));

                                break;
                            }
                        }
                    }
                }
            }

        }

        return permission;
    }
}
